<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use memberModel;

class memberscorelogModel extends Model
{
    protected $table = 'member_score_log';
    protected $primaryKey = 'id';
    protected $appends = ['event_text', 'oper_type_text'];

    protected static array $events = [
        'backofficechange'=>'后台变更',
        'signin'=>'签到奖励',
        'transfer'=>'积分转账',
        'orderpay'=>'订单支付',
        'refund'=>'退款',
        'orderaward'=>'订单奖励',
        'share'=>'分享邀请奖励',
    ];

    protected static array $operTypes = [
        'admin'=>'管理员',
        'system'=>'系统',
        'user'=>'用户',
    ];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return string
     */
    public function getEventTextAttribute()
    {
        $event_text = '';
        $event_key = array_keys(self::$events);
        if(in_array($this->attributes['event'], $event_key)){
            $event_text = self::$events[$this->attributes['event']];
        }
        unset($event_key);
        return $event_text;
    }

    /**
     * @return string
     */
    public function getOperTypeTextAttribute()
    {
        $oper_type_text = '';
        $oper_type_key = array_keys(self::$operTypes);
        if(in_array($this->attributes['oper_type'], $oper_type_key)){
            $oper_type_text = self::$operTypes[$this->attributes['oper_type']];
        }
        unset($oper_type_key);
        return $oper_type_text;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function getMember()
    {
        return $this->hasOne(memberModel::class, 'id', 'member_id')->select(['id', 'phone', 'nickname', 'realname', 'avatar']);
    }
}